package com.xbongbong.paas.toolbox.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.xbongbong.paas.toolbox.pojo.DataPermissionParamsPojo;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Objects;

public class TransmittableThreadLocalUtil {
    public static final TransmittableThreadLocal<String> THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static final TransmittableThreadLocal<String> CONNECTOR_THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static final TransmittableThreadLocal<String> WORK_FLOW_OPERATE_THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static void setValue(Integer saasMark, Integer businessType, Long menuId, Integer distributorMark) {
        distributorMark = distributorMark == null ? DistributorMarkEnum.OTHER.getCode() : distributorMark;
        DataPermissionParamsPojo dataPermissionParamsPojo = new DataPermissionParamsPojo(saasMark, XbbRefTypeEnum.transferBusinessType4Distributor(businessType, distributorMark), menuId, distributorMark);
        THREAD_LOCAL.set(JSON.toJSONString(dataPermissionParamsPojo));
    }

    public static void setValue(Integer saasMark, Integer businessType, Long menuId, Integer distributorMark, String corpid, String userId, Long formId) {
        distributorMark = distributorMark == null ? DistributorMarkEnum.OTHER.getCode() : distributorMark;
        DataPermissionParamsPojo dataPermissionParamsPojo = new DataPermissionParamsPojo(saasMark, XbbRefTypeEnum.transferBusinessType4Distributor(businessType, distributorMark), menuId, distributorMark, corpid, userId, formId );
        THREAD_LOCAL.set(JSON.toJSONString(dataPermissionParamsPojo));
    }

    public static void setValue(String str) {
        THREAD_LOCAL.set(str);
    }

    public static String getValue() {
        return THREAD_LOCAL.get();
    }

    public static DataPermissionParamsPojo getDataPermissionParamsPojoValue() {
        String str = THREAD_LOCAL.get();
        DataPermissionParamsPojo dataPermissionParamsPojo;
        try {
            dataPermissionParamsPojo = JSON.parseObject(str, DataPermissionParamsPojo.class);
        } catch (Exception e) {
            dataPermissionParamsPojo = new DataPermissionParamsPojo();
        }
        return dataPermissionParamsPojo;
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }

    public static void setConnectorValue() {
        CONNECTOR_THREAD_LOCAL.set("1");
    }

    public static String getConnectorValue() {
        return CONNECTOR_THREAD_LOCAL.get();
    }

    public static void removeConnectorValue() {
        CONNECTOR_THREAD_LOCAL.remove();
    }

    public static boolean isConnector() {
        String connectorValue = CONNECTOR_THREAD_LOCAL.get();
        removeConnectorValue();
        return Objects.equals(connectorValue, "1");
    }

    public static void setWorkFlowOperateValue(String operate) {
        WORK_FLOW_OPERATE_THREAD_LOCAL.set(operate);
    }

    public static String getWorkFlowOperateValue() {
        return WORK_FLOW_OPERATE_THREAD_LOCAL.get();
    }

    public static void removeWorkFlowOperateValue() {
        WORK_FLOW_OPERATE_THREAD_LOCAL.remove();
    }
}
